package com.yiba.vpn.authorization.manager.impl;

import com.yiba.vpn.authorization.manager.TokenManager;
import com.yiba.vpn.config.Config;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 通过Redis存储和验证token的实现类
 *
 * @author ScienJus
 * @date 2015/7/31.
 * @see com.yiba.vpn.authorization.manager.TokenManager
 */
@Component
public class RedisTokenManager implements TokenManager {

    private static Logger logger = Logger.getLogger(RedisTokenManager.class);

    @Autowired
    private Config config;


    private RedisTemplate<String, String> redis;

    /**
     *increment就是自增后的新知值
     * @param key
     * @param liveTime
     * @return
     */
    public Long incr(String key, long liveTime) {
        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redis.getConnectionFactory());
        Long increment = entityIdCounter.getAndIncrement();

        if ((null == increment || increment.longValue() == 0) && liveTime > 0) {//初始设置过期时间
            entityIdCounter.expire(liveTime, TimeUnit.SECONDS);
        }
        return increment;
    }



    @Autowired
    public void setRedis(RedisTemplate redisTemplate) {
        this.redis = redisTemplate;
        //设置序列化Key的实例化对象
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置序列化Value的实例化对象
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

    }



    public void deleteKey(String key) {
        redis.delete(key);
    }

    public void expireValue(String key, long time, TimeUnit timeUnit) {
        //如果验证成功，说明此用户进行了一次有效操作，延长token的过期时间
        redis.boundValueOps(key).expire(time, timeUnit);
    }


    /**
     * 存储key~value
     *
     * @param key
     * @param value
     * @param time
     */
    public void addValue(String key, String value, long time, TimeUnit timeUnit) {
        redis.boundValueOps(key).set(value, time, timeUnit);
    }

    /**
     * 存储key~value
     *
     * @param key
     * @param value
     * @param time
     */
    public void addV(String key, String value, long time, TimeUnit timeUnit) {
        redis.boundValueOps(key).set(value,time, timeUnit);
    }


    /**
     * 根据key获取参数值
     *
     * @param key
     * @return
     */
    public String getValue(String key) {
        return redis.boundValueOps(key).get();
    }

    /**
     * 根据key获取参数值  的返回为Long
     *
     * @param key
     * @return
     */
    public Long getInValue(String key) {

        RedisAtomicLong entityIdCounter = new RedisAtomicLong(key, redis.getConnectionFactory());
        Long increment = entityIdCounter.get();
        System.out.println(key +" ; " +increment);
        return increment;
    }


    /**
     * 获取redis剩余生存时间(如果过期返回-2，其他都返回0)
     */
    public Long ttlKey(String key) {
        return redis.getExpire(key);
    }


    /**
     * 指定时间类型---秒
     *
     * @param key
     * @return
     */
    public long getExpireTimeType(String key) {
        long time = redis.getExpire(key, TimeUnit.SECONDS);
        return time;
    }

}
